package com.project.controller.shopController;

import com.project.bean.GoodsBean;
import com.project.bean.ShopCarBean;
import com.project.service.GoodsService.IGoodsService;
import com.project.service.ShopService.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopCarService;
    @Autowired
    private IGoodsService goodsService;

    @RequestMapping("/purchaseAdd")
    public Collection<ShopCarBean> purchaseAdd(GoodsBean goodsBean, HttpServletRequest request, Integer num,String image){
        Map<Integer, ShopCarBean> shopMap = (Map<Integer, ShopCarBean>) request.getSession().getAttribute("purchaseShopMap");
        if(goodsBean != null){
            goodsBean.setImage(image);
            shopCarService.add(goodsBean, shopMap, num);
        }
        return shopMap.values();
    }

    @RequestMapping("/saleAdd")
    public Collection<ShopCarBean> saleAdd(Integer goodsId, HttpServletRequest request){
        Map<Integer, ShopCarBean> shopMap = (Map<Integer, ShopCarBean>) request.getSession().getAttribute("saleShopMap");
        if(goodsId != null){
            GoodsBean goods = goodsService.findById(goodsId);
            shopCarService.add(goods, shopMap, 0);
        }
        return shopMap.values();
    }

    @RequestMapping("/purchaseDel")
    public Collection<ShopCarBean> purchaseDel(int goodsId, HttpServletRequest request){
        Map<Integer, ShopCarBean> shopMap = (Map<Integer, ShopCarBean>) request.getSession().getAttribute("purchaseShopMap");
        shopCarService.del(goodsId, shopMap);
        return shopMap.values();
    }

    @RequestMapping("/saleDel")
    public String saleDel(int goodsId, HttpServletRequest request){
        Map<Integer, ShopCarBean> shopMap = (Map<Integer, ShopCarBean>) request.getSession().getAttribute("saleShopMap");
        shopCarService.del(goodsId, shopMap);
        return "ok";
    }

    @RequestMapping("/purchaseUpdate")
    public Collection<ShopCarBean> purchaseUpdate(int goodsId, HttpServletRequest request, Integer number) {
        Map<Integer, ShopCarBean> shopMap = (Map<Integer, ShopCarBean>) request.getSession().getAttribute("purchaseShopMap");
        shopCarService.update(goodsId, shopMap, number);
        return shopMap.values();
    }

    @RequestMapping("/saleUpdate")
    public String saleUpdate(int goodsId, HttpServletRequest request, Integer number) {
        Map<Integer, ShopCarBean> shopMap = (Map<Integer, ShopCarBean>) request.getSession().getAttribute("saleShopMap");
        shopCarService.update(goodsId, shopMap, number);
        return "ok";
    }

}
